<?php
namespace app\common\model;
class Statistics extends Common {
	protected $insert = ['addtime' => SYS_TIME];
	protected function getAddtimeAttr($value) {
		return date("Y-m-d H:i", $value);
	}
	protected function getMinfoAttr($value, $data) {
		return model('AdminUser')->where('userid', $data['userid'])->field('userid, realname, avatar')->find();
	}
	protected function getAlldataAttr($value, $data) {
		$data['realname'] = model('AdminUser')->where('userid', $data['userid'])->value('realname');
		$data['dealer'] = model('AdminUser')->get_dealer($data['userid']);
		trace($data['realname']);
		return $data;
	}
	protected function getDealerAttr($value, $data) {
		return model('AdminUser')->get_dealer($data['userid']);
	}
	//添加数据
	public function save_data($data) {
		//allowField过滤post数组中的非数据表字段数据
		$result = $this->allowField(true)->isUpdate(false)->data($data)->save();
		if ($result !== false) {
			$pk = $this->getPk();
			$id = $this->$pk;
			return $id;
		}
		return false;
	}
	//增加数量
	public function add_log($userid, $kind = 1, $clients = 1, $price = 0, $orders = 1, $suc_clients = 0) {
		trace($userid);
		//每天排名
		$nowday = date("Y-m-d", SYS_TIME);
		$count_day = $this->where('userid', $userid)->where('kind', $kind)->where('addday', $nowday)->where('typeid', 1)->count();
		if (!$count_day) {
			$data = array(
				'userid' => $userid,
				'kind' => $kind,
				'clients' => $clients,
				'price' => $price,
				'orders' => $orders,
				'suc_clients' => $suc_clients,
				'typeid' => 1,
				'addday' => $nowday,
			);
			$this->save_data($data);
		} else {
			$this->where('userid', $userid)->where('kind', $kind)->where('addday', $nowday)->where('typeid', 1)->inc('clients', $clients)->inc('price', $price)->inc('orders', $orders)->inc('suc_clients', $suc_clients)->update();
		}
		//每周排名
		$nowweek = date("Y", SYS_TIME) . "-" . date('W') . "-W";
		$count_week = $this->where('userid', $userid)->where('kind', $kind)->where('addday', $nowweek)->where('typeid', 2)->count();
		if (!$count_week) {
			$data = array(
				'userid' => $userid,
				'kind' => $kind,
				'clients' => $clients,
				'price' => $price,
				'orders' => $orders,
				'suc_clients' => $suc_clients,
				'typeid' => 2,
				'addday' => $nowweek,
			);
			$this->save_data($data);
		} else {
			$this->where('userid', $userid)->where('kind', $kind)->where('addday', $nowweek)->where('typeid', 2)->inc('clients', $clients)->inc('price', $price)->inc('orders', $orders)->inc('suc_clients', $suc_clients)->update();
		}

		//每月排名
		$nowmou = date("Y-m", SYS_TIME);
		$count_mou = $this->where('userid', $userid)->where('kind', $kind)->where('addday', $nowmou)->where('typeid', 3)->count();
		if (!$count_mou) {
			$data = array(
				'userid' => $userid,
				'kind' => $kind,
				'clients' => $clients,
				'price' => $price,
				'orders' => $orders,
				'suc_clients' => $suc_clients,
				'typeid' => 3,
				'addday' => $nowmou,
			);
			$this->save_data($data);
		} else {
			$this->where('userid', $userid)->where('kind', $kind)->where('addday', $nowmou)->where('typeid', 3)->inc('clients', $clients)->inc('price', $price)->inc('orders', $orders)->inc('suc_clients', $suc_clients)->update();
		}
		//总排名
		$count = $this->where('userid', $userid)->where('kind', $kind)->where('typeid', 4)->count();
		if (!$count) {
			$data = array(
				'userid' => $userid,
				'kind' => $kind,
				'clients' => $clients,
				'price' => $price,
				'orders' => $orders,
				'suc_clients' => $suc_clients,
				'typeid' => 4,
				'addday' => date("Y-m-d", SYS_TIME),
			);
			$this->save_data($data);
		} else {
			$this->where('userid', $userid)->where('kind', $kind)->where('typeid', 4)->inc('clients', $clients)->inc('price', $price)->inc('orders', $orders)->inc('suc_clients', $suc_clients)->update();
		}
		return true;
	}
	public function list_datas($addday, $userid, $roleid, $puserid, $mysubordid = 0) {
		$list = [];
		$uinfo = [];
		switch ($roleid) {
		case 9:
			$uinfo = [$userid];
			$stat_userid = $userid;
			$kind = 1;
			break;
		case 8:
			$userids = model('AdminUser')->get_all_childs($userid, $roleid, $puserid);
			$underlist = model('AdminUser')->where('puserid', 'in', $userids)->order('userid desc')->select();
			foreach ($underlist as $v) {
				$uinfo[] = $v['userid'];
			}
			$stat_userid = $userid;
			$kind = 2;
			break;
		case 10:
			$userids = model('AdminUser')->get_all_childs($userid, $roleid, $puserid);
			$underlist = model('AdminUser')->where('puserid', 'in', $userids)->order('userid desc')->select();
			foreach ($underlist as $v) {
				$uinfo[] = $v['userid'];
			}
			$stat_userid = $userid;
			$kind = 2;
			break;
		case 11:
			$userids = model('AdminUser')->get_all_childs($userid, $roleid, $puserid);
			$underlist = model('AdminUser')->where('puserid', 'in', $userids)->order('userid desc')->select();
			foreach ($underlist as $v) {
				$uinfo[] = $v['userid'];
			}
			$stat_userid = $userid;
			$kind = 2;
			break;
		case 7:
			$stat_userid = model('AdminUser')->where('userid', $userid)->value('puserid');
			$underlist = model('AdminUser')->where('puserid', $stat_userid)->where('roleid', 8)->order('userid desc')->select();
			foreach ($underlist as $v) {
				$uinfo[] = $v['userid'];
			}
			$kind = 3;
			break;
		case 5:
			$stat_userid = model('AdminUser')->where('userid', $userid)->value('puserid');
			$underlist = model('AdminUser')->where('puserid', $stat_userid)->where('roleid', 6)->order('userid desc')->select();
			foreach ($underlist as $v) {
				$uinfo[] = $v['userid'];
			}
			$kind = 4;
			break;
		}
		$stat_userid = $mysubordid ? $mysubordid : $stat_userid;
		$info = $this->where('addday', $addday)->where('kind', $kind)->where('userid', $stat_userid)->field('clients,price,orders,suc_clients')->find();
		if ($roleid != 9) {
			$list['client'] = $this->where('userid', 'in', $uinfo)->where('addday', $addday)->where('kind', $kind - 1)->order('clients desc')->select();
			$list['price'] = $this->where('userid', 'in', $uinfo)->where('addday', $addday)->where('kind', $kind - 1)->order('price desc')->select();
			$list['order'] = $this->where('userid', 'in', $uinfo)->where('addday', $addday)->where('kind', $kind - 1)->order('orders desc')->select();
			$list['suc_clients'] = $this->where('userid', 'in', $uinfo)->where('addday', $addday)->where('kind', $kind - 1)->order('suc_clients desc')->select();
		}
		return [$info, $list, $uinfo];
	}
	protected function getRolenameAttr() {
		$status = model('adminUser')->alias('u')->join('sj_auth_group g', 'u.roleid = g.id', 'RIGHT')->select();
		trace(odel('adminUser')->getlastsql());

	}
}